package com.ksyzt.ksite2.client.picturecenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RequiresResize;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.gwt.client.mainframe.AppData;
import com.ksyzt.gwt.client.mainframe.IAppModule;
import com.ksyzt.gwt.client.mainframe.ModuleProperties;
import com.ksyzt.ksite2.client.column.ColumnTree;
import com.ksyzt.ksite2.client.news.PictureList;
import com.ksyzt.ksite2.shared.OrgType;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;

public class PictureFrame extends MessageComposite implements RequiresResize,
	IAppModule
{

    private static OrgnizationEditorUiBinder uiBinder = GWT
	    .create(OrgnizationEditorUiBinder.class);

    interface OrgnizationEditorUiBinder extends UiBinder<Widget, PictureFrame>
    {
    }

    public PictureFrame()
    {
	initWidget(uiBinder.createAndBindUi(this));

	tree.addMessageHandler(m_tree_handler);
	tree.setSize("100%", "100%");
    }

    @UiField
    DockLayoutPanel root;

    boolean m_onlyone;

    public void initData(int userid, int orgtype, boolean onlyone)
    {
	m_userid = userid;
	m_orgtype = orgtype;
	m_onlyone = onlyone;
	tree.initTree(m_userid, m_orgtype, OrgType.getOrgNameByType(m_orgtype),
		onlyone);

    }

    MessageHandler m_tree_handler = new MessageHandler()
    {

	public void onMessage(Object sender, Integer message, Object value)
	{
	    if (MessageEvent.CREATEROOT.equals(message))
	    {
		edit(m_userid, m_orgtype, null, null, null);
	    } else if (MessageEvent.ITEMCLICK.equals(message))
	    {
		TreeItem ti = (TreeItem) value;
		COLUMNObj o = (COLUMNObj) ti.getUserObject();

		COLUMNObj p = null;
		TreeItem tip = tree.getTreeItem(o.getPID());

		if (tip != null && tip.getUserObject() != null)
		{
		    p = (COLUMNObj) tip.getUserObject();
		}

		edit(m_userid, m_orgtype, ti, p, o);
	    }
	}
    };

    NodeEditor editor;

    private void edit(int userid, int orgtype, TreeItem treeitem,
	    COLUMNObj parent, COLUMNObj org)
    {
	sureEditor();
	editor.edit(userid, orgtype, treeitem, parent, org);
	
	editor.showflash();
    }

    Widget current = null;

    private MessageHandler m_editor_handler = new MessageHandler()
    {

	@Override
	public void onMessage(Object sender, Integer message, Object value)
	{
	    if (message == MessageEvent.UPDATE)
	    {
		initData(m_userid, m_orgtype, m_onlyone);
	    } else if (message == MessageEvent.EDITCONTENT)
	    {
		COLUMNObj column = (COLUMNObj) value;
		surePictureList();
		list.setUserId(m_userid);
		list.initList(column);
	    }

	}
    };

    PictureList list;

    private MessageHandler m_list_handler = new MessageHandler()
    {
	@Override
	public void onMessage(Object sender, Integer message, Object value)
	{
	    if (MessageEvent.ITEMCLICK.equals(message))
	    {

	    } else if (MessageEvent.REFRESH.equals(message))
	    {

	    }
	}
    };

    private void surePictureList()
    {
	if (list == null)
	{
	    list = new PictureList();
	    list.addMessageHandler(m_list_handler);
	}
	if (current != list)
	{
	    if (current != null)
	    {
		current.removeFromParent();
	    }
	    current = list;
	    root.add(current);
	}

    }

    private void sureEditor()
    {
	if (editor == null)
	{
	    editor = new NodeEditor();
	    editor.addMessageHandler(m_editor_handler);
	}
	if (current != editor)
	{
	    if (current != null)
	    {
		root.remove(current);
	    }
	    root.add(editor);
	    current = editor;
	    
	}

    }

    @UiField
    ColumnTree tree;

    COLUMNObj m_org;

    TreeItem m_treeitem;
    int m_userid;
    int m_orgtype;

    HandlerRegistration handler;

    public static String KEY_ORGTYPE = "key_orgtype";
    public static String KEY_ONLYONE = "key_only";

    @Override
    public void onInitialize(AppData application, ModuleProperties pros)
    {
	handler = this.addMessageHandler(application.messageHandler);

	int userid = application.user.id;
	int orgtype = pros.getInt(KEY_ORGTYPE, OrgType.OT_PICTURE);
	GWT.log("init orgtype" + orgtype);
	boolean onlyone = pros.getBoolean(KEY_ONLYONE, true);
	initData(userid, orgtype, onlyone);

    }

    @Override
    public void onUnInitialize()
    {
	handler.removeHandler();
    }

    @Override
    public Widget getRootWidget()
    {
	return this;
    }

    @Override
    public void onResize()
    {

    }

}
